package com.example.hzq.zqb.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ListView;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.GrandActivity;
import com.example.hzq.zqb.adapter.GridAdapter;
import com.example.hzq.zqb.adapter.ListAdapter;
import com.example.hzq.zqb.bean.GrandBean;
import com.example.hzq.zqb.model.Model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/13.
 */

public class Fragment2 extends Fragment {
    private ListView listView;
    private ImageButton scan;
    private View view=null;
    private Context context;
    private int aid;
    private GridView gridview;
    private List<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
    private List<GrandBean>list=null;
    private GridAdapter gridAdapter;
    private ListAdapter listAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment2,container,false);
        context=getActivity();
        aid=1;
        scan=(ImageButton)view.findViewById(R.id.f2_scan);
        listView=(ListView)view.findViewById(R.id.list);
        gridview=(GridView)view.findViewById(R.id.gridview);
        listAdapter=new ListAdapter(context);
        listView.setAdapter(listAdapter);
        getdata(aid);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              getdata(position+1);
            }
        });
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }

        });
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GrandBean grandBean=list.get(position);
                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putSerializable("grand",grandBean);
                intent.putExtras(bundle);
                intent.setClass(context, GrandActivity.class);
                context.startActivity(intent);
            }
        });
        return view;
    }

    private void getdata(int aid){
        lstImageItem.clear();
        Model model=new Model();
        Call<List<GrandBean>>call=model.typeList(0,aid);
        call.enqueue(new Callback<List<GrandBean>>() {
            @Override
            public void onResponse(Call<List<GrandBean>> call, Response<List<GrandBean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    for (int i=0;i<response.body().size();i++){
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("ItemImage",imageurl+response.body().get(i).getImg());//添加图像资源的ID
                        map.put("ItemText",response.body().get(i).getName());//按序号做ItemText
                        lstImageItem.add(map);
                    }
                    gridAdapter=new GridAdapter(context,lstImageItem);
                    gridview.setAdapter(gridAdapter);
                  /*  //生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
                    SimpleAdapter saImageItems = new SimpleAdapter(context, //没什么解释
                            lstImageItem,//数据来源
                            R.layout.griditem,//grid_item的XML实现
                            //动态数组与ImageItem对应的子项
                            new String[] {"gridimage","gridtext"},
                            //ImageItem的XML文件里面的1个ImageView,1个TextView ID
                            new int[] {R.id.gridimage,R.id.gridtext});
                    //添加并且显示
                    gridview.setAdapter(saImageItems);*///只适用于本地图片
                }
            }
            @Override
            public void onFailure(Call<List<GrandBean>> call, Throwable t) {
            }
        });
    }
}
